package App.Repository;

import App.Entity.Task;
import App.Entity.TaskReceive;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * Created by yu on 2017/7/20.
 */
@Repository
public interface TaskCheckRepository extends CrudRepository<TaskReceive,Long>
{
    @Query("select taskId from TaskReceive item where item.projectId=:projectId and item.receiverId=:receiverId")
    public List<Long> getTaskIdByProjectIdAndReceiverId(@Param("projectId")Long projectId,@Param("receiverId")Long receiverId);

    @Transactional
    @Modifying(clearAutomatically = true)
    @Query("update TaskReceive item set item.status=:status where item.taskId=:taskId and item.receiverId=:receiverId")
    public void setTaskStatusByTaskIdAndReceiverId(@Param("taskId") Long taskId,@Param("receiverId") Long receiverId,@Param("status")int status);

    public TaskReceive findByTaskIdAndReceiverId(@Param("taskId") Long taskId,@Param("receiverId") Long receiverId);

    @Query("select taskId from TaskReceive item where item.receiverId=:authId and item.status=1")
    public List<Long> getRunningTaskByAuthId(@Param("authId") Long authId);

    @Query("select taskId from TaskReceive item where item.receiverId=:authId and item.status=0")
    public List<Long> getFinishedTaskByAuthId(@Param("authId") Long authId,Pageable pageable);

    public List<TaskReceive> findByProjectIdAndStatusOrderByReceiveTimeDesc(Long projectId,int status);

    public List<TaskReceive> findByProjectIdAndStatusOrderByReceiveTimeDesc(Long projectId,int status,Pageable pageable);

    public List<TaskReceive> findByProjectIdAndStatusOrderByModifyTimeDesc(Long projectId,int status);

    public List<TaskReceive> findByProjectIdAndStatusOrderByModifyTimeDesc(Long projectId,int status,Pageable pageable);

    public List<TaskReceive> findByProjectIdAndStatusAndModifyTimeLessThanOrderByModifyTimeDesc(Long projectId,int status,long modifyTime,Pageable pageable);

    public List<TaskReceive> findByProjectIdAndModifyTimeGreaterThanOrderByModifyTimeDesc(long projectId,long modifyTime);

    @Query("select taskId from TaskReceive item where item.projectId=:projectId and item.receiverId=:receiverId and item.status=:status")
    public List<Long> getTaskIdsByProjectIdAndReceiverIdAndStatus(@Param("projectId")Long projectId,@Param("receiverId")Long receiverId,@Param("status")int status);

    @Query("select count(id) from TaskReceive item where item.projectId=:projectId and item.status=0")
    public Long getFinishTaskCountByProjectId(@Param("projectId") Long projectId);

    @Query("select count(id) from TaskReceive item where item.projectId=:projectId and item.receiverId=:authId and item.status=:status")
    public Long getTasksCountByAuthIdAndProjectIdAndStatus(@Param("projectId") Long projectId,@Param("authId")Long authId,@Param("status")int status);



}
